package config

import (
	"github.com/BurntSushi/toml"
	"fmt"
)

const CONFIG_FILE string = "conf/app.toml"

var Config *Conf

type Conf struct {
	Server 			*HttpConfig 			`toml:"http"`
	Redis 			map[string]RedisConfig 	`toml:"redis"`
	DBs 			map[string]DBConfig		`toml:"db"`
	Log 			logConfig 				`toml:"log"`
}

func NewConfig() *Conf {
	c := &Conf{
		Server: 	NewHttpConfig(),
		Redis: 		make(map[string]RedisConfig),
		DBs: 		make(map[string]DBConfig),
	}
	err := InitConfig(c)
	if err != nil {
		panic("config init error")
	}
	return c
}

func (c Conf) GetMainDBConfig() DBConfig {
	return c.DBs["main"]
}

// 初始化配置信息
func InitConfig(c *Conf) error {
	if _, err := toml.DecodeFile(CONFIG_FILE, &c); err != nil {
		return fmt.Errorf("read config file failed, err: %v", err)
	}
	return nil
}
